/************************************************************************/
/*                                                                      */
/*                      Software Development Kit                        */
/*              Copyright (C) M-Systems Ltd. 1995-2003                  */
/*                                                                      */
/************************************************************************/

#include "flbase.h"

/* #define FL_CREATE_THREAD */ /* Mutex implemntation with OS help */

/*----------------------------------------------------------------------*/
/*                    f l S y s f u n I n i t                           */
/*                                                                      */
/* Perform system initialization.                                       */
/*                                                                      */
/* Note : This example file did not support a valid implementation.     */
/*----------------------------------------------------------------------*/

void flSysfunInit(void)
{
	/* Initialize system resource */
	DBG_PRINT_WRN(FLZONE_BLKDEV,"\r\nTrueFFS says hello.\r\n");
}


/*----------------------------------------------------------------------*/
/*                    f l S y s f u n E x i t                           */
/*                                                                      */
/* Free any system resources taken by TrueFFS.                          */
/*                                                                      */
/* This routine was added in TtueFFS 6.0 and is prorotyped in           */
/* flsystems.h using the FL_SYS_FUNC_RELEASE macro.                     */
/*                                                                      */
/* Note : This example file did not support a valid implementation.     */
/*----------------------------------------------------------------------*/

void flSysfunExit()
{
	/* Free system resource */
	DBG_PRINT_WRN(FLZONE_BLKDEV,"\r\nTrueFFS says goodbye.\r\n");
}



/*----------------------------------------------------------------------*/
/*                     f l C r e a t e M u t e x                        */
/*                                                                      */
/* Creates or initializes a mutex                                       */
/*                                                                      */
/* Parameters:                                                          */
/*      mutex           : Pointer to mutex                              */
/*                                                                      */
/* Returns:                                                             */
/*      FLStatus        : 0 on success, otherwise failure               */
/*----------------------------------------------------------------------*/

FLStatus flCreateMutex(FLMutex *mutex)
{
#ifdef FL_CREATE_THREAD
	PVoid mtx = CreateMutex(NULL, FALSE, NULL); //FALSE means no immediate ownership
	if (!mtx) return flGeneralFailure;

	*mutex = (FLMutex)mtx;
#else /* FL_CREATE_THREAD */
	*mutex = 0;
#endif /* FL_CREATE_THREAD */

	return flOK;
}

/*----------------------------------------------------------------------*/
/*                     f l D e l e t e M u t e x                        */
/*                                                                      */
/* Deletes a mutex.                                                     */
/*                                                                      */
/* Parameters:                                                          */
/*      mutex           : Pointer to mutex                              */
/*                                                                      */
/*----------------------------------------------------------------------*/

void flDeleteMutex(FLMutex *mutex)
{
#ifdef FL_CREATE_THREAD
	CloseHandle((PVoid)*mutex);
#endif /* FL_CREATE_THREAD */
}

/*----------------------------------------------------------------------*/
/*                      f l T a k e M u t e x                           */
/*                                                                      */
/* wait for mutex                                                       */
/*                                                                      */
/* Parameters:                                                          */
/*      mutex           : Pointer to mutex                              */
/*                                                                      */
/* Returns:                                                             */
/*      int             : TRUE = Mutex taken, FALSE = failure           */
/*----------------------------------------------------------------------*/

FLBoolean flTakeMutex(FLMutex *mutex)
{
#ifdef FL_CREATE_THREAD
	return (WaitForSingleObject((PVoid)*mutex, INFINITE) == WAIT_OBJECT_0);
#else /* FL_CREATE_THREAD */
	(*mutex)++;
	if (*mutex > 1) {
		(*mutex)--;
		return FALSE;
	}

	return TRUE;
#endif /* FL_CREATE_THREAD */
}

/*----------------------------------------------------------------------*/
/*                        f l F r e e M u t e x                         */
/*                                                                      */
/* Free mutex.                                                          */
/*                                                                      */
/* Parameters:                                                          */
/*      mutex           : Pointer to mutex                              */
/*                                                                      */
/*----------------------------------------------------------------------*/

void flFreeMutex(FLMutex *mutex)
{
#ifdef FL_CREATE_THREAD
	ReleaseMutex((PVoid)*mutex);
#else
	(*mutex)--;
#endif /* FL_CREATE_THREAD */
}


/*----------------------------------------------------------------------*/
/*                        f l R a n d B y t e                           */
/*                                                                      */
/* Return a random number from 0 to 255.                                */
/*                                                                      */
/* Note : This example file did not support a valid implementation.     */
/*----------------------------------------------------------------------*/

unsigned flRandByte(void)
{
	return 0;
}

/*----------------------------------------------------------------------*/
/*                         f l C u r r e n t T i m e                    */
/*                                                                      */
/* Return current DOS time.                                             */
/*                                                                      */
/* The DOS time field is encoded as follows:                            */
/*    bit 0-4   : Seconds divided by 2 (0-29)                           */
/*    bit 5-10  : Minutes (0-59)                                        */
/*    bit 11-15 : Hours (0-23)                                          */
/*                                                                      */
/* Note : This example file did not support a valid implementation.     */
/*----------------------------------------------------------------------*/

unsigned flCurrentTime(void)
{
	return 0;
}

/*----------------------------------------------------------------------*/
/*                         f l C u r r e n t D a t e                    */
/*                                                                      */
/* Return current DOS date.                                             */
/*                                                                      */
/* The DOS date field is encoded as follows:                            */
/*    bit 0-4   : Day of month (1-31)                                   */
/*    bit 5-8  : Month (1-12)                                           */
/*    bit 9-15 : Year relative to 1980                                  */
/*                                                                      */
/* Note : This example file did not support a valid implementation.     */
/*----------------------------------------------------------------------*/

unsigned flCurrentDate(void)
{
	return 0;
}
